package com.huaqin.punan.turnsilent;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.media.AudioManager;
import android.os.IBinder;
import android.preference.PreferenceManager;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;

import java.util.List;

public class TurnSilentService extends Service {
    private TelephonyManager telephonyMgr; 
    private SensorManager sensorMgr; 
    private AudioManager audioMgr; 
    private SensorEventListener sensorListener; 
    private PhoneCallListener phoneListener; 
    private boolean ringing = false;
    @Override
    public IBinder onBind(Intent arg0) {
        // TODO Auto-generated method stub
        return null;
    }
    @Override
    public void onCreate() {
        // TODO Auto-generated method stub
        super.onCreate();
        Sensor orientation = null;
        SharedPreferences mSharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
        telephonyMgr = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
        sensorMgr = (SensorManager)this.getSystemService(SENSOR_SERVICE);
        audioMgr = (AudioManager)this.getSystemService(AUDIO_SERVICE); 
        phoneListener = new PhoneCallListener();
        telephonyMgr.listen(phoneListener, PhoneCallListener.LISTEN_CALL_STATE);
        sensorListener = new SensorListener();
        
        List<Sensor> sensorList = sensorMgr.getSensorList(Sensor.TYPE_ALL);        
        int size = sensorList.size();
        for (int i = 0; i<size ;i++) {            
            int type = sensorList.get(i).getType();
            if (type == Sensor.TYPE_ACCELEROMETER) {
                orientation = sensorList.get(i);
                break;
            }
        }
        
        if (orientation != null) { 
            sensorMgr.registerListener(sensorListener,orientation, SensorManager.SENSOR_DELAY_NORMAL);
        } else { 
            stopSelf();
        }
        
    }
    @Override
    public void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        telephonyMgr.listen(phoneListener, 0);
        sensorMgr.unregisterListener(sensorListener);
    }

    private class PhoneCallListener extends PhoneStateListener{
        @Override
        public void onCallStateChanged(int state, String incomingNumber) {
            // TODO Auto-generated method stub
            switch (state) {
                case TelephonyManager.CALL_STATE_IDLE: //电话处于待机状态
                  ringing = false;
                  break;
                case TelephonyManager.CALL_STATE_OFFHOOK://电话通话中
                  ringing = false;
                  break;
                case TelephonyManager.CALL_STATE_RINGING://有电话来电
                  ringing = true;
                  break;
                }
            super.onCallStateChanged(state, incomingNumber);
        }
    }
    
    class SensorListener implements SensorEventListener{

        @Override
        public void onAccuracyChanged(Sensor arg0, int arg1) {
            // TODO Auto-generated method stub
            
        }

        @Override
        public void onSensorChanged(SensorEvent event) {
            // TODO Auto-generated method stub
            if (event.values[2] < 0 && ringing) {  //电话正在响铃中，且手机面朝下
            	int ringerMode = AudioManager.RINGER_MODE_SILENT;
                audioMgr.setRingerMode(ringerMode);
              }else{
                audioMgr.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
              }
        }
    }
    
}
